package com.skyolder.milkbee.service;

import com.skyolder.milkbee.domain.Winner;
import com.skyolder.milkbee.repository.WinnerRepository;
import com.skyolder.milkbee.service.dto.WinnerDTO;
import com.skyolder.milkbee.service.mapper.WinnerMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing Winner.
 */
@Service
@Transactional
public class WinnerService {

    private final Logger log = LoggerFactory.getLogger(WinnerService.class);
    
    @Inject
    private WinnerRepository winnerRepository;

    @Inject
    private WinnerMapper winnerMapper;

    /**
     * Save a winner.
     *
     * @param winnerDTO the entity to save
     * @return the persisted entity
     */
    public WinnerDTO save(WinnerDTO winnerDTO) {
        log.debug("Request to save Winner : {}", winnerDTO);
        Winner winner = winnerMapper.winnerDTOToWinner(winnerDTO);
        winner = winnerRepository.save(winner);
        WinnerDTO result = winnerMapper.winnerToWinnerDTO(winner);
        return result;
    }

    /**
     *  Get all the winners.
     *  
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Transactional(readOnly = true) 
    public Page<WinnerDTO> findAll(Pageable pageable) {
        log.debug("Request to get all Winners");
        Page<Winner> result = winnerRepository.findAll(pageable);
        return result.map(winner -> winnerMapper.winnerToWinnerDTO(winner));
    }

    /**
     *  Get one winner by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Transactional(readOnly = true) 
    public WinnerDTO findOne(Long id) {
        log.debug("Request to get Winner : {}", id);
        Winner winner = winnerRepository.findOne(id);
        WinnerDTO winnerDTO = winnerMapper.winnerToWinnerDTO(winner);
        return winnerDTO;
    }

    /**
     *  Delete the  winner by id.
     *
     *  @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete Winner : {}", id);
        winnerRepository.delete(id);
    }

	public Page<WinnerDTO> findByuserId(Long userId, Pageable pageable) {
		// TODO Auto-generated method stub
		  Page<Winner> result = winnerRepository.findByuserId(userId,pageable);
	      return result.map(winner -> winnerMapper.winnerToWinnerDTO(winner));
	}
}
